<?php
/**
 * Created by PhpStorm.
 * User: nhson219
 * Date: 9/10/14
 * Time: 3:41 PM
 */
class Milestone_model extends Model {

    /**
     * Constructor
     *
     */
    function Messages_model()
    {
        parent::Model();
    }//Controller End

    // --------------------------------------------------------------------

    /* Get Users List */
    function getCateMilestone()
    {
        $this->db->select('id,title');
        $this->db->from('milestone_categories');
        $this->db->where('is_deleted',0);
        return $this->db->get();

    }



    function addProjectsMilestones($insertData=array())
    {
        $this->db->insert('projects_milestones', $insertData);

    }//End of getGroups Function

    /**
     * updateMailnotification
     *
     * @access	private
     * @param	array	an associative array of insert values
     * @return	void
     */
    function updateMilestone($id=0,$updateData=array(),$conditions=array(),$conditions_in = array(),$conditions_notin = array())
    {
        if($id != 0)
            $this->db->where('id',$id);

        if(is_array($conditions) and count($conditions)>0)
            $this->db->where($conditions);


        if(is_array($conditions_in) and count($conditions_in) > 0)
            $this->db->where_in('id',$conditions_in);

        if(is_array($conditions_notin) and count($conditions_notin) > 0)
            $this->db->where_not_in('id',$conditions_notin);

        //echo $this->db->_compile_select();exit;
        $this->db->update('projects_milestones', $updateData);


    }//End of updateMilestone Function

    // --------------------------------------------------------------------


    /**
     * Get Project Messages
     *
     * @access	private
     * @param	array	conditions to fetch data
     * @return	object	object with result set
     */
    function getMilestoneCate($conditions=array(),$fields='',$like=array(),$limit=array(),$order=array())
    {
        //Check For Conditions
        if(count($conditions)>0)
            $this->db->where($conditions);

        if(is_array($like) and count($like)>0)
            $this->db->like($like);

        //Check For Limit
        if(is_array($limit))
        {
            if(count($limit)==1)
                $this->db->limit($limit[0]);
            else if(count($limit)==2)
                $this->db->limit($limit[0],$limit[1]);
        }
        if(is_array($order) and count($order)>0)
            $this->db->orderby($order[0],$order[1]);
        else
            $this->db->orderby('milestones.order','asc');

        $this->db->from('milestones');
        $this->db->join('milestone_categories', 'milestone_categories.id = milestones.category_id','left');
        //Check For Fields
        if($fields!='')
            $this->db->select($fields);
        else
            $this->db->select('milestone_categories.id,milestones.title,milestones.return_fee,milestone_categories.title');

        $result = $this->db->get();
        //pr($result->result());exit;
        return $result;

    }//End of getMilestoneCate Function

    function getProjectsMilestone($conditions=array(),$fields='',$like=array(),$limit=array(),$order=array())
    {
        //Check For Conditions
        if(count($conditions)>0)
            $this->db->where($conditions);

        if(is_array($like) and count($like)>0)
            $this->db->like($like);

        //Check For Limit
        if(is_array($limit))
        {
            if(count($limit)==1)
                $this->db->limit($limit[0]);
            else if(count($limit)==2)
                $this->db->limit($limit[0],$limit[1]);
        }
        if(is_array($order) and count($order)>0)
            $this->db->orderby($order[0],$order[1]);
        else
            $this->db->orderby('projects_milestones.order','asc');

        $this->db->from('projects_milestones');
        $this->db->join('milestone_categories', 'milestone_categories.id = projects_milestones.milestone_cateId','left');
        $this->db->join('projects', 'projects.id = projects_milestones.projects_id','left');
        //Check For Fields
        if($fields!='')
            $this->db->select($fields);
        else
            $this->db->select('projects_milestones.id,projects_milestones.title,projects_milestones.fee');

        $result = $this->db->get();
        //pr($result->result());exit;
        return $result;

    }//End of getProjectMessages Function

    function insertCustomMilestone($data = array(),$projects_id = null,$category_id = null,$type = 0){
        if(is_array($data) && $projects_id != null ){
            $insert = "";
            $dem = count($data);
            if($type == 1)
                $dem = $dem - 1;
            foreach($data as $k => $v){
                if($dem == $k)
                    $comma = "";
                else
                    $comma = ",";

            if($category_id == null)
                $insert .= "('0','".$v['name']."','".$projects_id."','".$v['fee']."','".$v['order']."')".$comma;
            else
                $insert .= "('".$category_id."','".$v['name']."','".$projects_id."','".$v['fee']."','".$v['order']."')".$comma;

            }
            $query = "Insert into `projects_milestones` (`milestone_cateId`,`name`,`projects_id`,`fee`,`order`) values  {$insert} ";
        }else{
            return false;
        }
        $this->db->query($query);
    }
    function removeOneMilestone($condition = array()){
        if(is_array($condition))
            $this->db->where($condition);

        return $this->db->delete('projects_milestones');
    }
    function sumMilestone($condition = array()){
        $this->db->select_sum('fee');

        if(is_array($condition))
            $this->db->where($condition);

        return $this->db->get('projects_milestones')->row();
    }



/* End of file Milestones_model.php */
/* Location: ./app/models/Milestones_model.php */
}
?>